<?php


Class SendmailComponent extends EmailComponent{
    var $activateMailing=true;
    var $to=null;//'ProfileManagement.ALW@airliquide.com';
    var $cc = array('ProfileManagement.ALW@airliquide.com');
    var $subject = 'ALW Profile management alert';
    var $template = 'default';
    var $smtp=false;
    var $replyTo = 'ProfileManagement.ALW@airliquide.com';
    var $from = 'ProfileManagement.ALW@airliquide.com';
    var $sendAs = 'both';
    var $model=null;
    var $msgUser=null;
    var $msg=null;
    var $status=null;
    var $MailTemplates=array(
        1 => 'created',
        2 => 'received',
        3 => 'pending_it',
        4 => 'it_done',
        5 => 'pending_eico',
        6 => 'eico_done',
        7 => 'pending_demander',
        8 => 'demander_done',
        9 => 'problem',
        10 => 'closed'
    );
    
    var $dmdformCode=null;
    var $dmdformId=null;
    var $demander=null;
    var $dmdType=null;

    function newMail($status,$dmdCode,$dmdId,$dmdtype=null){
        $this->model=$this->Controller->modelNames[0];
        $this->dmdformCode=$dmdCode;
        $this->status=$status;
        $this->dmdformId=$dmdId;
        if($dmdtype){$this->dmdType=$dmdtype;}
        if($this->_getMail($status)){
          if ($this->activateMailing){
            (!$this->smtp)? $this->_sendMail():$this->_sendMailSmtp(0);
          }
        }else{
            $this->Controller->$model->createAction(3,$this->dmdformId,$this->Controller->Auth->user('id'),  $this->status);
        }
    }
    function customMail($to,$message,$template=null,$subject=null){
        $this->to=$to;
        $this->msgUser=$message;
        ($template)?$this->template=$template:$this->template='custom';
        ($subject)?$this->subject=$subject:$this->subject=$this->subject;
        $this->dmdformId=0;
        $this->model='Dmdform';
        $this->status=0;
        $this->_sendMailCustomSmtp(0);
    }
    function _checkManager(){
        $model=$this->model;
       // if($model!='Dmdform'){$model=$this->Controller->$model->Dmdform;}
        //debug($model);
        $c=  $this->Controller->$model->getCheckMail($this->dmdformId);
        //debug($c);
        $mid= $c['mid'];
        
        $hrid=$c['hrid'];
        
        $val=$c['val'];        
        
        if ($mid!=$this->Controller->Auth->user('id')){
            $this->cc=array_merge($this->cc,array($this->to));
            $this->to=$this->Controller->$model->getUserMail($mid);  
            $this->msg='Manager Validation Required !';
        }elseif ($mid==$this->Controller->Auth->user('id')){                    
            $this->cc=array_merge($this->cc,array($this->Controller->$model->getUserMail($hrid)));
            if ($val==0){
              $this->msg='Manager Validation Required !';  
            }
        }else{
            $this->cc=array_merge($this->cc,array($this->Controller->$model->getUserMail($hrid),$this->Controller->$model->getUserMail($mid)));
        }
    }

    function _getMail($status=null){
        $model=$this->model;
      
         if($status){
             $this->template=$this->MailTemplates[$status];             
             $this->subject=  $this->subject.' : '.$this->dmdformCode;
             switch ($status){
                 case 1:
                     $this->to=  $this->Controller->$model->getUserMail($this->Controller->Auth->user('id'));                     
                     $this->demander=$this->Controller->$model->getUser($this->Controller->Auth->user('id'));
                     $this->_checkManager();
                     return true;
                     break;
                 case 2: 
                     $this->to=$this->Controller->$model->getDemanderMail($this->dmdformId);
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     $this->_checkManager();
                     return true;
                     break;
                 case 3:
                     $ta=$this->Controller->$model->getItMailsForTools($this->dmdformId);
                     $this->to=$ta[0];
                     $this->cc=array_merge($this->cc,$ta);
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     $this->_checkManager();
                     return true;
                     break;
                 case 4:                     
                     $this->to=$this->Controller->$model->getDemanderMail($this->dmdformId);                    
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     $this->_checkManager();
                     return true;
                     break;
                 case 5:case 9:
                     $this->to=$this->Controller->$model->getMailsForGroup(3);
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     return true;
                     break;
                 case 8:
                     $this->to=  $this->replyTo;
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     return true;
                     break;
                 case 7:case 6:
                     $this->to=$this->Controller->$model->getDemanderMail($this->dmdformId);
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     return true;
                     break;
                 case 10:
                     $this->to=$this->Controller->$model->getDemanderMail($this->dmdformId);
                     $this->demander=$this->Controller->$model->getDemanderName($this->dmdformId);
                     return true;
                     break;
             }
             ($status==1)?$actionType=1:$actionType=2;
             $this->Controller->$model->createAction($actionType,$this->dmdformId,$this->Controller->Auth->user('id'),$status);
        }else{
            return false;
        }
    }
    function _sendMail() {
        $model=$this->model;
            //$User = $this->User->read(null,$id);
        if($this->msgUser){
            $this->Controller->set('msgUser', $this->msgUser);
        }
        $this->Controller->set('dmdformCode',$this->dmdformCode);
        $this->Controller->set('dmdformId', $this->dmdformId);
        $this->Controller->set('demander', $this->demander);
        $this->Controller->set('dmdType', $this->dmdType);
        $this->Controller->set('msg', $this->msg);
                
            $this->send();
         if(empty($this->smtpError)){
            $this->Controller->$model->createAction(3,$this->dmdformId,$this->Controller->Auth->user('id'),  $this->status);
            return true;
        }else{
            $this->Controller->$model->createAction(4,$this->dmdformId,$this->Controller->Auth->user('id'),  $this->status);
        }
         }

    function _sendMailSmtp($ty=1){
        $model=$this->model;
        if ($ty==1){
            $this->smtpOptions = array(
                'port'=>'465',
                'timeout'=>'30',
                'host' => 'ssl://smtp.gmail.com',
                'username'=>'youness.mondir',
                'password'=>'youn1096',
            );
        }else{
            $this->smtpOptions = array(
                'port'=>'25',
                'timeout'=>'300',
                'host' => '172.26.249.241',
            );
        }
       
        $this->delivery = 'smtp';
        if($this->msgUser){
            $this->Controller->set('msgUser', $this->msgUser);
        }
        $this->Controller->set('dmdformCode',$this->dmdformCode);
        $this->Controller->set('dmdformId', $this->dmdformId);
        $this->Controller->set('demander', $this->demander);
        $this->Controller->set('dmdType', $this->dmdType);
        $this->Controller->set('msg', $this->msg);
        
        $this->send();

        
        if(empty($this->smtpError)){
            $this->Controller->$model->createAction(3,$this->dmdformId,$this->Controller->Auth->user('id'),  $this->status);
            return true;
        }else{
            $this->Controller->$model->createAction(4,$this->dmdformId,$this->Controller->Auth->user('id'),  $this->status);
        }

    }
    function _sendMailCustomSmtp($ty=1){
        $model=$this->model;
        if ($ty==1){
            $this->smtpOptions = array(
                'port'=>'465',
                'timeout'=>'30',
                'host' => 'ssl://smtp.gmail.com',
                'username'=>'youness.mondir',
                'password'=>'youn1096',
            );
        }else{
            $this->smtpOptions = array(
                'port'=>'25',
                'timeout'=>'300',
                'host' => '172.26.249.241',
            );
        }
       
        $this->delivery = 'smtp';
        if($this->msgUser){
            $this->Controller->set('msgUser', $this->msgUser);
        }
        
        
        $this->send();
        if(empty($this->smtpError)){
            $this->Controller->$model->createAction(5,0,$this->Controller->Auth->user('id'),  0);
            return true;
        }else{
            $this->Controller->$model->createAction(5,0,$this->Controller->Auth->user('id'),  0);
        }
        
    }
        


}
?>
